package sort;

public class InsertionSort {

	public static void main(String[] args){
		int[] arr = {9,5,8,7,3,2,4};
		ascendSort(arr);
		for (int i : arr) {
			System.out.print(i + "  ");
		}
		System.out.println();
		descendSort(arr);
		for (int i : arr) {
			System.out.print(i + "  ");
		}
	}
	
	private static void ascendSort(int[] arr){
		for(int j = 1 ; j <  arr.length;j++){
			int key = arr[j];
			int i = j - 1;
			while(i >= 0 && arr[i] > key){
				arr[i+1] = arr[i];
				i = i - 1;
			}
			arr[i+1] = key;
		}
	}
	
	private static void descendSort(int[] arr){
		for(int j = 1 ; j <  arr.length;j++){
			int key = arr[j];
			int i = j - 1;
			while(i >= 0 && arr[i] < key){
				arr[i+1] = arr[i];
				i = i - 1;
			}
			arr[i+1] = key;
		}
	}
	
}
